package com.charm.information.ui.fragment

import android.os.Bundle
import android.support.v4.app.ActivityOptionsCompat
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.charm.codes.mvp.factory.CreatePresenter
import com.charm.codes.mvp.view.AbstractFragment
import com.charm.information.R
import com.charm.information.adapter.EastHeadLineAdapter
import com.charm.information.constract.EastHeadLineView
import com.charm.information.constract.presenter.EastHeadLinePresenter
import com.charm.information.entity.EastItemEntity
import com.ethanhua.skeleton.RecyclerViewSkeletonScreen
import com.ethanhua.skeleton.Skeleton
import com.scwang.smartrefresh.layout.SmartRefreshLayout

/**
 * Created by tools on 2018/1/13.
 */
@CreatePresenter(EastHeadLinePresenter::class)
@Route(path = "/information/eastHeadLine")
class EastHeadlineFragment : AbstractFragment<EastHeadLineView, EastHeadLinePresenter>(), EastHeadLineView {
    private lateinit var refresh: SmartRefreshLayout
    lateinit var rv: RecyclerView
    private var page = 1
    private var catid = ""
    private lateinit var adapter: EastHeadLineAdapter
    var skeletonScreen: RecyclerViewSkeletonScreen? = null
    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        var rootView = inflater!!.inflate(R.layout.fragment_east_headline, container, false)
        initView(rootView)
        return rootView
    }

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        catid = arguments.get("catid") as String
        initListener()
        initData()
    }

    private fun initData() {
        mvpPresenter.eastHeadLineList(page,catid)
//        rv.adapter = adapter
    }

    private fun initListener() {
        adapter = EastHeadLineAdapter(null)
//        adapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT)
        skeletonScreen = Skeleton.bind(rv)
                .adapter(adapter)
                .shimmer(true)
                .angle(20)
                .frozen(false)
                .duration(1200)
                .count(10)
                .load(R.layout.item_skeleton)
                .show()
        refresh.setOnRefreshListener {
            page = 1
            adapter.data.clear()
            mvpPresenter.eastHeadLineList(page, catid)
        }

        adapter.setOnLoadMoreListener({
            page++
            mvpPresenter.eastHeadLineList(page, catid)
        }, rv)
        adapter.setOnItemClickListener { adapter, view, position ->
            var image = view.findViewById<ImageView>(R.id.image)
            val optionsCompat = ActivityOptionsCompat
                    .makeSceneTransitionAnimation(
                            activity,
                            image,
                            "eastHeadLine")
            var entity = adapter.data[position]
            ARouter.getInstance()
                    .build("/information/eastHeadLine/detail")
                    .withOptionsCompat(optionsCompat)
                    .withObject("entity", entity)
                    .greenChannel()
                    .navigation(activity)
        }
    }

    private fun initView(rootView: View) {
        refresh = rootView.findViewById(R.id.refresh)
        rv = rootView.findViewById(R.id.rv)
        rv.layoutManager = LinearLayoutManager(context)
    }

    override fun eastHeadLineList(list: List<EastItemEntity>) {
        adapter.addData(list)
        netComplete()
    }

    override fun netComplete() {
        skeletonScreen!!.hide()
        if (refresh.isRefreshing) {
            refresh.finishRefresh()
        }
        if (adapter.isLoading) {
            adapter.loadMoreComplete()
        }
    }
}